<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		width="1000px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-10 flex-end-center">
			<jk-search-input :value="formData.workshopName" disabled class="search-bar-item-size1 item-gap" placeholder="区域"></jk-search-input>
			<jk-search-input :value="formData.productName" disabled class="search-bar-item-size3 item-gap" placeholder="品种编号或名称" @change="onSearchBtn"></jk-search-input>
			<jk-search-input v-model="searchParams.batchCode" class="search-bar-item-size2 item-gap" placeholder="批号" @on-enter="onSearchBtn"></jk-search-input>
			<jk-search-input v-model="searchParams.productOrderCode" class="search-bar-item-size3 item-gap" placeholder="生产订单号" @on-enter="onSearchBtn"></jk-search-input>
			<jk-search-input v-model="searchParams.code" show-search class="search-bar-item-size3" placeholder="单据编号" @on-enter="onSearchBtn"></jk-search-input>
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="dialogTableHeight - 30"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<vxe-table-column field="productName" title="品种" fixed="left" :width="180"></vxe-table-column>
			<vxe-table-column field="batchCode" title="批号" fixed :width="140"></vxe-table-column>
			<vxe-table-column field="productModels" title="规格型号" fixed :width="140"></vxe-table-column>
			<vxe-table-column field="productionQty" title="排产数量" sortable align="right" :width="110" />
			<vxe-table-column field="code" title="通知单编号" sortable :min-width="140" />
			<vxe-table-column field="batchDate" title="生产订单" :width="140">
				<template slot-scope="scope">
					<span>{{ scope.row.productOrder ? scope.row.productOrder.code : '' }}</span>
				</template>
			</vxe-table-column>
			<vxe-table-column field="planStartDate" title="计划开始交货日期" sortable :width="140" />
			<vxe-table-column field="planFinishDate" title="计划结束交货日期" sortable :width="140" />
			<vxe-table-column field="workshopName" title="区域" :width="100" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="searchParams.count"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../../../mixins/cal-dialog-height';
    import { pageConfig } from '../../../../../../utils/jk/jk-tool';
    import { productionPrdNoticePage } from '../../../../../../api/production/xsNotice';

    export default {
        mixins: [calDialogHeight],
        props: {
            formData: {
                type: Object,
                default() {
                    return {};
                }
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: '细纱通知单单'
            }
        },
        data() {
            return {
                searchParams: {
                    count: 0,
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    batchCode: '',
                    code: '',
                    productOrderCode: '',
                    productName: null,
                    workshopId: null
                },
                selectRow: null,
                searchBarProcessId: null,
                searchBarNameOrCode: '',
                pageNo: 1,
                pageSize: pageConfig.pageSize,
                totalCount: null,
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectData).length) {
                    this.$emit('on-confirm', this.selectData);
                } else {
                    this.$tipsBar(this, 'unChoose');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                this.selectData = [];
                return productionPrdNoticePage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.$set(this.searchParams, 'count', res.data.count);
                        this.tableData = res.data.res;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                const cpData = this.formData ? JSON.parse(JSON.stringify(this.formData)) : {};
                // this.$set(this.searchParams, 'productId', cpData.productId);
                this.$set(this.searchParams, 'workshopId', cpData.workshopId);
                // this.$set(this.searchParams, 'productName', cpData.productName);
                await this.getListRequest();
            }
        },
        watch: {
            formData: {
                handler(newVal) {
                    // const cpData = newVal ? JSON.parse(JSON.stringify(newVal)) : {};
                    // this.$set(this.searchParams, 'productName', cpData.productName);
                },
                deep: true,
                immediate: true
            },
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
